import { NextResponse } from "next/server";

export async function POST(request: Request) {
  try {
    const body = await request.json();
    const { query, projectPhase, documentType, projectManager, dateRange } =
      body;

    // 这里替换为实际的数据库查询逻辑
    // 示例返回数据
    const mockResults = [
      {
        id: "1",
        title: "2025年XX项目设计变更申请",
        projectName: "XX项目",
        documentType: "设计变更",
        projectPhase: "设计",
        projectManager: "张三",
        createTime: "2025-01-15",
        previewUrl: "/documents/1",
      },
      {
        id: "2",
        title: "XX项目施工方案评审会议纪要",
        projectName: "XX项目",
        documentType: "会议纪要",
        projectPhase: "施工",
        projectManager: "李四",
        createTime: "2025-02-20",
        previewUrl: "/documents/2",
      },
      {
        id: "3",
        title: "YY项目竣工验收报告",
        projectName: "YY项目",
        documentType: "验收报告",
        projectPhase: "验收",
        projectManager: "王五",
        createTime: "2025-03-10",
        previewUrl: "/documents/3",
      },
      {
        id: "4",
        title: "ZZ项目运维手册",
        projectName: "ZZ项目",
        documentType: "施工方案",
        projectPhase: "运维",
        projectManager: "赵六",
        createTime: "2025-04-05",
        previewUrl: "/documents/4",
      },
      {
        id: "5",
        title: "AA项目规划方案",
        projectName: "AA项目",
        documentType: "设计变更",
        projectPhase: "规划",
        projectManager: "钱七",
        createTime: "2025-05-01",
        previewUrl: "/documents/5",
      },
      {
        id: "6",
        title: "BB项目施工签证单",
        projectName: "BB项目",
        documentType: "签证单",
        projectPhase: "施工",
        projectManager: "孙八",
        createTime: "2025-06-15",
        previewUrl: "/documents/6",
      },
      {
        id: "7",
        title: "CC项目设计评审会议纪要",
        projectName: "CC项目",
        documentType: "会议纪要",
        projectPhase: "设计",
        projectManager: "周九",
        createTime: "2025-07-20",
        previewUrl: "/documents/7",
      },
      {
        id: "8",
        title: "DD项目运维方案",
        projectName: "DD项目",
        documentType: "施工方案",
        projectPhase: "运维",
        projectManager: "吴十",
        createTime: "2025-08-25",
        previewUrl: "/documents/8",
      },
    ];

    // 模拟搜索逻辑
    const filteredResults = mockResults.filter((result) => {
      const matchesQuery =
        !query ||
        result.title.toLowerCase().includes(query.toLowerCase()) ||
        result.projectName.toLowerCase().includes(query.toLowerCase());

      const matchesPhase =
        !projectPhase || result.projectPhase === projectPhase;
      const matchesType = !documentType || result.documentType === documentType;
      const matchesManager =
        !projectManager ||
        result.projectManager
          .toLowerCase()
          .includes(projectManager.toLowerCase());

      const resultDate = new Date(result.createTime);
      const matchesDateRange =
        (!dateRange.start || resultDate >= new Date(dateRange.start)) &&
        (!dateRange.end || resultDate <= new Date(dateRange.end));

      return (
        matchesQuery &&
        matchesPhase &&
        matchesType &&
        matchesManager &&
        matchesDateRange
      );
    });

    return NextResponse.json(filteredResults);
  } catch (error) {
    console.error("Search error:", error);
    return NextResponse.json({ error: "搜索请求处理失败" }, { status: 500 });
  }
}
